package org.argus.job.server.system.workflow.validator;

import org.argus.common.core.enums.StatusEnum;
import org.argus.common.core.exception.ServiceException;
import org.argus.job.common.enums.WorkflowNodeType;
import org.argus.job.server.system.domain.JobInfoEntity;
import org.argus.job.server.system.domain.WorkflowNodeInfoEntity;
import org.argus.job.server.system.management.mapper.JobInfoMapper;
import org.argus.job.server.system.workflow.algorithm.WorkflowDAG;
import org.springframework.stereotype.Component;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Echo009
 * @since 2021/12/14
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class JobNodeValidator implements NodeValidator {

    private final JobInfoMapper jobInfoMapper;

    @Override
    public void complexValidate(WorkflowNodeInfoEntity node, WorkflowDAG dag) {
        // do nothing
    }

    @Override
    public void simpleValidate(WorkflowNodeInfoEntity node) {
        // 判断对应的任务是否存在
        JobInfoEntity job = jobInfoMapper.selectById(node.getJobId());
        if (job == null) {
            throw new ServiceException("Illegal job node,specified job is not exist,node name : " + node.getNodeName());
        }

        if (job.getStatus() == StatusEnum.DELETED) {
            throw new ServiceException(
                "Illegal job node,specified job has been deleted,node name : " + node.getNodeName());
        }
    }

    @Override
    public WorkflowNodeType matchingType() {
        return WorkflowNodeType.JOB;
    }
}
